/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * solucionBusqueda.java
 *
 * Created on 08/05/2011, 17:15:07
 */

package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Mauricio
 */
public class solucionBusqueda extends javax.swing.JFrame {
    JPanelImage[][] Cuadro; //VARIABLE QUE REPRESENTA UN CUADRO DEL TABLERO
    //Parámetros:
    int profundidadMaximaAlcanzada;
    int cantidadNodosGenerados;
    long tiempoProcesamiento;
    boolean termino;
    int nivelSolucion;
    String [][] ocupaciones1;
    List<int []> posicionesRecibidas;
    int [][] matrizTablero;
    int algo;
    int costofRecibidos;


    public solucionBusqueda(int [][] matrizTablero, List<int[]> posicionesRecibidas, String nombreAlgoritmos, int algo , int prof, int cantN, long tiempo, boolean termino, int nivSol, int costof) {
        
        initComponents();
        //algo: 1 profundidad, 2 amplitud, 3 a estrella
        this.setLocationRelativeTo(null);

        //PRIMERO ARMAMOS UNA LISTA CON LOS VALORES DE FILA/COLUMNA DE LAS POSICIONES A PINTAR

        for(int k=0;k<posicionesRecibidas.size();k++){
            int[] elm;
            elm = posicionesRecibidas.get(k);
            int fila = elm[0];
            int col = elm[1];
            System.out.println("Posicion: "+fila+" "+col);
        }
        this.posicionesRecibidas = posicionesRecibidas;
        this.profundidadMaximaAlcanzada = prof;
        this.cantidadNodosGenerados = cantN;
        this.tiempoProcesamiento = tiempo;
        this.termino = termino;
        this.nivelSolucion = nivSol;
        this.jLabel3.setText(nombreAlgoritmos);
        this.jLabel5.setText(String.valueOf(posicionesRecibidas.size()));
        this.matrizTablero = matrizTablero;
        this.algo = algo;
        this.costofRecibidos = costof;
        String[][] ocupaciones;
        ocupaciones = new String[8][8];
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){
                ocupaciones[i][j] = "a";
            }
        }

        for(int k=0;k<posicionesRecibidas.size();k++){
            int[] elm;
            elm = posicionesRecibidas.get(k);
            int fila;
            int col;
            fila = elm[0];
            col = elm[1];
            int f =0 ;
            if(algo==3){
                //significa que es a* y hay que imprimir el valor de f'
                f = elm[2];
            }

            if(ocupaciones[fila][col].equals("a")){
                if(algo==3){

                    ocupaciones[fila][col] = String.valueOf(elm[2]);

                }else{

                String valor;
                valor = String.valueOf(k);
                ocupaciones[fila][col] = valor;

                }

            }else{
                if(algo==3){

                String texto;
                texto = ocupaciones[fila][col];
                String concat;
                concat = texto + " " + String.valueOf(elm[2]);
                ocupaciones[fila][col] = concat;
                }else{

                if(algo==1){
                
                }else{

                String texto;
                texto = ocupaciones[fila][col];
                String concat;
                concat = texto + " " + String.valueOf(k);
                ocupaciones[fila][col] = concat;

                }

                }
            }
            
        }
        this.ocupaciones1 = ocupaciones;

        //SE PINTA EN EL TABLERO EL ESTADO INICIAL
        GridLayout layout = new GridLayout();
        layout.setRows(8);
        layout.setColumns(8);
        layout.setHgap(1);
        layout.setVgap(1);
        this.tableroPanel.setLayout(layout);
        Cuadro = new JPanelImage[8][8];
        this.show();

        for(int x=0;x<8;x++){
            for(int y=0;y<8;y++){

                if(x%2==0){
                    if(y%2!=0)
                    {
                    Cuadro[x][y]=new JPanelImage();
                    
                        if(!ocupaciones[x][y].equals("a")){
                        JLabel label = new JLabel();
                        label.setText(ocupaciones[x][y]);
                        Cuadro[x][y].add(label);
                        }
                     
                    Cuadro[x][y].caballo_blanco("cuadronegro");                    
                    this.tableroPanel.add(Cuadro[x][y]);
                    }
                    else
                    {

                    Cuadro[x][y]=new JPanelImage();
                    
                        if(!ocupaciones[x][y].equals("a")){
                        JLabel label = new JLabel();
                        label.setText(ocupaciones[x][y]);
                        Cuadro[x][y].add(label);
                    }
                     
                    Cuadro[x][y].caballo_blanco("cuadroblanco");
                    this.tableroPanel.add(Cuadro[x][y]);
                    }
                }else{
                    if(y%2==0)
                    {
                    Cuadro[x][y]=new JPanelImage();
                    
                     if(!ocupaciones[x][y].equals("a")){
                        JLabel label = new JLabel();
                        label.setText(ocupaciones[x][y]);
                        Cuadro[x][y].add(label);
                    }
                     
                    Cuadro[x][y].caballo_blanco("cuadronegro");
                    this.tableroPanel.add(Cuadro[x][y]);
                    }
                    else
                    {
                    Cuadro[x][y]=new JPanelImage();
                   
                     if(!ocupaciones[x][y].equals("a")){
                        JLabel label = new JLabel();
                        label.setText(ocupaciones[x][y]);
                        Cuadro[x][y].add(label);
                    }
                     
                    Cuadro[x][y].caballo_blanco("cuadroblanco");
                    this.tableroPanel.add(Cuadro[x][y]);

                    }
                }
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tableroPanel = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Solución de la Búsqueda");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/iconos/cerrar.png"))); // NOI18N
        jButton3.setText("Cerrar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/iconos/ver_result.png"))); // NOI18N
        jButton1.setText("Resultados");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Solución del Problema"));

        jLabel2.setText("Algoritmo Usado:");

        jLabel3.setText("jLabel3");

        jLabel4.setText("Cantidad de Movimientos realizados:");

        jLabel5.setText("jLabel5");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)))
                .addContainerGap(118, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tableroPanel.setBackground(new java.awt.Color(102, 102, 102));
        tableroPanel.setPreferredSize(new java.awt.Dimension(400, 400));

        javax.swing.GroupLayout tableroPanelLayout = new javax.swing.GroupLayout(tableroPanel);
        tableroPanel.setLayout(tableroPanelLayout);
        tableroPanelLayout.setHorizontalGroup(
            tableroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        tableroPanelLayout.setVerticalGroup(
            tableroPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/iconos/camino.png"))); // NOI18N
        jButton2.setText("Ver Camino");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tableroPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 227, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3))
                    .addComponent(tableroPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //MOSTRAMOS LOS RESULTADOS DE LA BUSQUEDA
        String tipo = "";
        if(algo==1){
            tipo = "Costo";
        }
        if(algo==2){
            tipo = "Costo";
        }
        if(algo==3){
            tipo = "F'";
        }
        resultadoTablero open = new resultadoTablero(this.profundidadMaximaAlcanzada,this.cantidadNodosGenerados,this.tiempoProcesamiento,this.termino,this.nivelSolucion, tipo, this.costofRecibidos);
        open.show(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    this.jButton2.setEnabled(false);
    //Aca cargamos los caballos blancos y el negro
    for(int a=0;a<8;a++){
        for(int b=0;b<8;b++){

            if(this.matrizTablero[a][b]==1){
            this.Cuadro[a][b].caballo_blanco("caballo_blanco");
            this.Cuadro[a][b].repaint();
            }

            if(this.matrizTablero[a][b]==2){
                this.Cuadro[a][b].caballo_blanco("caballo_negro");
                this.Cuadro[a][b].repaint();

            }

        }
    }
        
        int [] posAnt;
        posAnt = new int[2];
        posAnt[0] = 0;
        posAnt[1] = 0;

        int [] posCN;
        posCN = posicionesRecibidas.get(0);
        posAnt[0] = posCN[0];
        posAnt[1] = posCN[1];

        //PINTAMOS EL RECORRIDO DEL CABALLO NEGRO
        for(int i=0;i<posicionesRecibidas.size();i++){

            int opcion = JOptionPane.showConfirmDialog(null, "Siguiente Posición", null, JOptionPane.YES_NO_OPTION);

            if(opcion==0){

            int[] pos;
            pos = posicionesRecibidas.get(i);
            this.Cuadro[posAnt[0]][posAnt[1]].caballo_blanco("cuadrorojo");
            this.Cuadro[posAnt[0]][posAnt[1]].repaint();
            this.Cuadro[pos[0]][pos[1]].caballo_blanco("caballo_negro");
            this.Cuadro[pos[0]][pos[1]].repaint();
            posAnt[0] = pos[0];
            posAnt[1] = pos[1];


            }else{
                break;
            }
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new solucionBusqueda().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel tableroPanel;
    // End of variables declaration//GEN-END:variables

}

